<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2019/1/7  20:30
 */
namespace app\api\logic;
use app\api\BaseModel;


class Driver extends  BaseModel
{

    //审核司机申请
    public function check()
    {
        $id = $this->requestData['id'];
        $status = $this->requestData['status'];
        if(!in_array($status,array(1,-1))){
            return ['message'=>'参数有误'];
        }
        $db=db("driver_shop");
        $data['status']=$status;
        $driver_id=$db->where("id='$id'")->find();
        if($driver_id['shop_id'] != $this->user['shopid'])return ['message'=>'参数有误'];
        $driver = db('driver')->where(['id'=>$driver_id['driver_id']])->find();
        if($status==1) {
            if ($driver['status'] == 0 || $driver['status'] == -1) {
                return ['message'=>'司机的状态异常'];
            }
        }
        if($driver_id['status'] ==1){return ['message'=>'审核已通过'];}

        $res=$db->where("id='$id'")->update($data);
//		var_dump($res);die;
        if($res){
            return array();
        }else{
            return ['message'=>'审核失败！'];
        }
    }

    //我的司机
    public function index()
    {
        $page=isset($this->requestData['page'])?$this->requestData['page']:'1';
        $limit=isset($this->requestData['limit'])?$this->requestData['limit']:'10';
//        if(isset($this->requestData['status'])){
            $where['status'] = [['=',0],['=',1],'or'];
//        }
//        if(isset($this->requestData['isbind'])){
            $where['isbind'] = 1;
//        }
        $where = array();
        $where['shop_id']=$this->user['shopid'];

        $list = db("driver_shop")->where($where)->order("id desc")->page($page,$limit)->select();
        $count = db("driver_shop")->where($where)->count();
        foreach($list as $key=>&$value){
            $value['driver'] = db('driver')->alias('d')
                ->join('driver_bankcard dc','d.id=dc.driver_id','LEFT')
                ->where(['d.id'=>$value['driver_id']])
                ->field('d.sex,d.province,d.city,county,d.street,d.address,d.nickname,d.headimage,d.mobile,d.idcard_number,d.cart_number,d.driving_number,d.driving_image,dc.card_num,dc.bank')->find();
            //证件
            if(!empty($value['driver']['driving_image'])){
                $value['driver']['driving_image'] = IMG_PATH_LICENSE.$value['driver']['driving_image'];
            }
            //司机平均分
            $grade = db('driver_order_comment')->where(['driver_id'=>$value['driver_id']])->avg('grade');
            $value['driver']['grade'] = $grade;
        }

        $result['list'] = $list;
        $data['count'] = $count;
        $data['page_num']=$page;
        $data['page_limit']=$limit;
        return $result;
    }


    //司机详情
    public function info()
    {
        $page=isset($this->requestData['page'])?$this->requestData['page']:'1';
        $limit=isset($this->requestData['limit'])?$this->requestData['limit']:'10';
        $id = isset($this->requestData['id'])?$this->requestData['id']:'0';
        if(!$id)return ['message'=>'参数必传!'];
        $condition['shop_id'] = $this->user['shopid'];
        $condition['id'] = $id;
        $res = db('driver_shop')->where($condition)->find();
        if(!$res)return ['message'=>'数据不存在！'];
        $info = db('driver')->where(['id'=>$res['driver_id']])->find();
        unset($info['passwd']);
        if(!$info) return ['message'=>'不存在该司机'];
        $info['headimage'] = IMG_PATH.$info['headimage'];
        $info['driving_image'] = IMG_PATH_LICENSE.$info['driving_image'];

        $info['grade'] = db('driver_order_comment')->where(['driver_id'=>$id])->avg('grade');
        $res['info'] = $info;
        $comment = db('driver_order_comment')->where(['driver_id'=>$id])->order('id desc')->page($page, $limit)->select();
        foreach($comment as $k=>&$val){
            $val['user'] = db('member')->where(['id'=>$val['user_id']])->value('nickname');
            $val['driver'] = db('driver')->where(['id'=>$val['driver_id']])->value('nickname');
            $val['creatime'] = date('Y-m-d H:i:s',$val['creatime']);
        }
        $res['comment'] = $comment;
        return $res;
    }

    /*
    * 解绑司机
    */
    public function unbind()
    {
        $uid = $this->user['shopid'];
        $id = $this->requestData['id'];
        if(!$id)return ['message'=>'参数必传!'];
        $condition['shop_id'] = $uid;
        $condition['id'] = $id;

        $res = db('driver_shop')->where($condition)->find();
        if(!$res)return ['message'=>'数据不存在！'];
        if($res['status'] != 1)return ['message'=>'你无权解绑该司机！'];//只有审核通过才能解绑
        if($res['isbind']!=1)return ['message'=>'你已经解绑该司机！'];
        $data = [
            'isbind'=>-2,
            'updatetime'=>TIMESTAMP
        ];
        if(db('driver_shop')->where(['id'=>$id])->update($data))return array();
        return ['message'=>'解绑失败！'];
    }

    //删除司机
    public function driverdel()
    {
        $uid = $this->user['shopid'];
        $id = $this->requestData['id'];
        if(!$id)return ['message'=>'参数必传!'];
        $condition['shop_id'] = $uid;
        $condition['id'] = $id;

        $res = db('driver_shop')->where($condition)->find();
        if(!$res)return ['message'=>'数据不存在！'];
        if($res['isbind'] == 1)return ['message'=>'你未解绑该司机！'];
        if($res['isbind'] == -1)return ['message'=>'你无权删除该司机！'];

        if(db('driver_shop')->where(['id'=>$id])->delete())return array();
        return ['message'=>'删除失败！'];
    }

}